<template>
  <div>
    <van-swipe :autoplay="3000">
      <van-swipe-item v-for="(image, index) in goodsinfo.spuImageItems" :key="index">
        <img :src="image.img" />
      </van-swipe-item>
    </van-swipe>

    <div class="goodsInfo adapt">
      <div class="goodsInfo-left">
        <p class="title">{{ goodsinfo.title }}</p>
        <div class="tags-out">
          <van-tag
            v-for="(item2, index) in goodsinfo.tags"
            :key="index"
            type="primary"
            class="tags"
          >
            {{ item2 }}
          </van-tag>
        </div>
        <p class="subtitle">{{ goodsinfo.description }}</p>
        <p class="price">￥{{ goodsinfo.price }}</p>
      </div>
    </div>
    <van-goods-action>
      <van-goods-action-icon icon="chat-o" text="客服" color="#ee0a24" />
      <van-goods-action-icon icon="chat-o" text="购物车" to="/shopcart" :badge="getCount" />
      <van-goods-action-button
        class="cartbutton"
        type="warning"
        text="加入购物车"
        @click=";($refs.overlayRef.popupShow = true), ($refs.overlayRef.buyMode = false)"
      />
      <van-goods-action-button
        class="cartbutton"
        type="danger"
        text="立即购买"
        @click=";($refs.overlayRef.popupShow = true), ($refs.overlayRef.buyMode = true)"
      />
    </van-goods-action>

    <goodsPopup :goodsinfo="goodsinfo" ref="overlayRef"></goodsPopup>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import goodsPopup from '@/components/Popup'

export default {
  name: 'Goods',
  props: ['id'],
  components: {
    goodsPopup,
  },
  computed: { ...mapGetters('shopcart', ['getCount']) },
  data() {
    return {
      goodsinfo: {},
    }
  },
  created() {
    this.getSpuDetail()
  },
  methods: {
    getSpuDetail() {
      this.$http.get(`spu/detail/${this.id}`).then(res => {
        this.goodsinfo = res.data

        if (this.goodsinfo.tags !== null && this.goodsinfo.tags !== '') {
          this.goodsinfo.tags = this.goodsinfo.tags.split('$')
        }
      })
    },
  },
}
</script>

<style scoped>
.goodsInfo {
  margin: 50px 0;
}
.goodsInfo p {
  padding-left: 20px;
  text-align: left;
}
.goodsInfo-left {
  float: left;
  width: 83%;
  padding-bottom: 50px;
}
.subtitle {
  color: rgb(95, 126, 151);
  font-size: 13px;
}
.tags-out {
  text-align: left;
  margin-left: 15px;
}
.tags {
  margin-left: 4px;
}
.price {
  color: rgb(62, 62, 131);
  font-size: 16px;
}
</style>
